package com.sinodata.bsm.cicp.finder.weblogic9;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.JmxUtil;

public class JdbcFinder extends Finder {

    private static final Logger logger = Logger.getLogger(JdbcFinder.class);

    /*@Override
    public List find(ResValue resValue, ResAttributeValValue[] attrVals, ResAttributeValue[] attributes) {
        List resources = new ArrayList();
        //MBeanServerConnection connection = JmxUtil.getConnection(JmxUtil.WEBLOGIC, "192.168.0.211", "7001", "weblogic","weblogic1");
        String ip = host;
        String port = attrVals[1].getAttrValue();
        ObjectName service;
        String serverName = attrVals[0].getAttrValue();
        JMXConnector connector = null;
        MBeanServerConnection connection = null;
        try {
            connector = JmxUtil.getConnection(JmxUtil.WEBLOGIC, ip + ":" + port);
            connection = connector.getMBeanServerConnection();
            service = new ObjectName("com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");
            ObjectName[] serverRuntimes = (ObjectName[]) connection.getAttribute(service, "ServerRuntimes");
            for (ObjectName objectName2 : serverRuntimes) {
                if (serverName.equals(objectName2.getKeyProperty("Name"))) {
                    Object jdbcServiceRuntime = connection.getAttribute(objectName2, "JDBCServiceRuntime");
                    ObjectName[] jdbcDataSourceRuntimeMBeans = (ObjectName[]) connection.getAttribute((ObjectName) jdbcServiceRuntime, "JDBCDataSourceRuntimeMBeans");
                    String attributeNames[] = new String[] { "Name" };
                    for (ObjectName objectName3 : jdbcDataSourceRuntimeMBeans) {
                        Object attrValues[] = null;
                        attrValues = JmxUtil.getAttribute(connection, objectName3, attributeNames);
                        List oneResource = CreateOneResource(serverName, attrValues[0].toString(), attributes);
                        resources.add(oneResource);
                    }
                }
            }
        } catch (Exception e) {
            logger.error("failed to find weblogic9 Jdbc" + e);
            resources = null;
        } finally {
            JmxUtil.closeConnector(connector);
        }
        return resources;
    }*/

    /* private List CreateOneResource(String serverName, String jdbcName, ResAttributeValue[] attributes) {
         List aRes = new ArrayList();
         ResValue res = new ResValue();
         ResAttributeValValue valValue = null;
         res.setIp(host); // host IP address
         res.setInternalId(host + "weblogic9" + serverName + jdbcName); // Internal ID for agent
         res.setName(serverName + "." + jdbcName);
         aRes.add(res);
         valValue = com.union.pub.util.CollectorUtil.getResAttrValValue(attributes[0].getId(), serverName);
         aRes.add(valValue);
         valValue = com.union.pub.util.CollectorUtil.getResAttrValValue(attributes[1].getId(), jdbcName);
         aRes.add(valValue);
         return aRes;
     }*/

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        //MBeanServerConnection connection = JmxUtil.getConnection(JmxUtil.WEBLOGIC, "192.168.0.211", "7001", "weblogic","weblogic1");
        String port = (String) params.get("PORT");
        ObjectName service;
        String serverName = (String) params.get("SERVERNAME");
        JMXConnector connector = null;
        MBeanServerConnection connection = null;
        try {
            connector = JmxUtil.getConnection(ip, Integer.parseInt(port));
            connection = connector.getMBeanServerConnection();
            service = new ObjectName("com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");
            ObjectName[] serverRuntimes = (ObjectName[]) connection.getAttribute(service, "ServerRuntimes");
            for (ObjectName objectName2 : serverRuntimes) {
                if (serverName.equals(objectName2.getKeyProperty("Name"))) {
                    Object jdbcServiceRuntime = connection.getAttribute(objectName2, "JDBCServiceRuntime");
                    ObjectName[] jdbcDataSourceRuntimeMBeans = (ObjectName[]) connection.getAttribute((ObjectName) jdbcServiceRuntime, "JDBCDataSourceRuntimeMBeans");
                    String attributeNames[] = new String[] { "Name" };
                    for (ObjectName objectName3 : jdbcDataSourceRuntimeMBeans) {
                        Object attrValues[] = null;
                        attrValues = JmxUtil.getAttribute(connection, objectName3, attributeNames);
                        String[] info = new String[5];
                        list.add(info);
                        info[0] = ip + "weblogic9" + serverName + attrValues[0].toString();
                        info[1] = serverName + "." + attrValues[0].toString(); //serverName + "." + jdbcName
                        info[2] = serverName + "." + attrValues[0].toString();
                        info[3] = serverName; //服务器名字
                        info[4] = attrValues[0].toString(); //JDBC名称
                    }
                }
            }
            return list;
        } catch (Exception e) {
            logger.error("failed to find weblogic9 Jdbc" + e);
            return new ArrayList<String[]>();
        } finally {
            JmxUtil.closeConnector(connector);
        }
    }
}
